package com.clubupc.modelo;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class ProxyBeans {

	private static Map<String, BaseBean> beans = new LinkedHashMap<String, BaseBean>();

	static {
		try {
			InputStream in = ProxyBeans.class.getClassLoader().getResourceAsStream("tables.properties");
			DataInputStream din = new DataInputStream(in);
			BufferedReader br = new BufferedReader(new InputStreamReader(din));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				strLine = strLine.trim();
				if (strLine.trim().length() == 0 || strLine.startsWith("#")) {
					continue;
				}
				StringTokenizer stk = new StringTokenizer(strLine, "=", false);
				StringTokenizer stk1 = new StringTokenizer(stk.nextToken().trim(), ".", false);
				String tbl = stk1.nextToken().trim();
				String key = stk1.nextToken().trim();
				String value = stk.nextToken().trim();
				if (beans.containsKey(tbl)) {
					Map<String, String> prop = beans.get(tbl).getConfig();
					prop.put(key, value);
				} else {
					LinkedHashMap<String, String> prop = new LinkedHashMap<String, String>();
					prop.put(key, value);
					beans.put(tbl, new BaseBean(tbl, prop));
				}
			}
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static BaseBean getBaseBean(String table) {
		return beans.get(table);
	}

}
